home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / (Sources) / Standard Controls / Text / XGStdGroupBox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  5.2 KB  |  235 lines

  1. /*    XGStdGroupBox.cpp
  2.  *
  3.  *        this is my internal implementation of a static text object.
  4.  *    This is something I simply roll myself
  5.  */
  6.  
  7. /*  YAAF - Yet another application framework
  8.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  9.  *  
  10.  *  This library is free software; you can redistribute it
  11.  *  and/or modify it under the terms of the GNU Library
  12.  *  General Public License as published by the Free Software
  13.  *  Foundation; either version 2 of the License, or any
  14.  *  later version.
  15.  *  
  16.  *  This library is distributed in the hope that it will be
  17.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  18.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  19.  *  PURPOSE. See the GNU Library General Public License for
  20.  *  more details.
  21.  *  
  22.  *  You should have received a copy of the GNU Library General
  23.  *  Public License along with this library; if not, write to the
  24.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25.  *  Boston, MA 02111-1307, USA.
  26.  *  
  27.  *  To contact the author, either e-mail me at
  28.  *  woody@alumni.caltech.edu, or write to us at
  29.  *  
  30.  *          William Edward Woody
  31.  *          In Phase Consulting
  32.  *          1545 Ard Eevin Avenue
  33.  *          Glendale, CA 91202
  34.  */
  35.  
  36. #include <string.h>
  37. #include <XError.h>
  38. #include <XDataUtil.h>
  39. #include <XStdText.h>
  40. #include <XApplication.h>
  41. #include <XStdControls.h>
  42.  
  43. /************************************************************************/
  44. /*                                                                        */
  45. /*    Construction/Destruction                                            */
  46. /*                                                                        */
  47. /************************************************************************/
  48.  
  49. /*    XGStdGroupBox::XGStdGroupBox
  50.  *
  51.  *        Initialize me
  52.  */
  53.  
  54. XGStdGroupBox::XGStdGroupBox(XGView *v, XGArgStream &s) : 
  55.     XGStdText(v,s,true)
  56. {
  57.     char buffer[256];
  58.     s.GetString(sizeof(buffer),buffer);
  59.     Init(buffer);
  60. }
  61.  
  62. XGStdGroupBox::XGStdGroupBox(XGView *v, XGSTextInitRecord &s) : 
  63.     XGStdText(v,s.v,true)
  64. {
  65.     Init(s.text);
  66. }
  67.  
  68.  
  69. /*    XGStdGroupBox::~XGStdGroupBox
  70.  *
  71.  *        Delete me
  72.  */
  73.  
  74. XGStdGroupBox::~XGStdGroupBox()
  75. {
  76. }
  77.  
  78. /************************************************************************/
  79. /*                                                                        */
  80. /*    Text Drawing                                                        */
  81. /*                                                                        */
  82. /************************************************************************/
  83.  
  84. /*    XGStdGroupBox::GetText
  85.  *
  86.  *        Get the text contents of this
  87.  */
  88.  
  89. void XGStdGroupBox::GetText(char *text)
  90. {
  91. #if OPT_MACOS == 1
  92.     strcpy(text,fString.Get());
  93. #endif
  94.  
  95. #if OPT_WINOS == 1
  96.     ::GetWindowText(_GetHWND(),text,256);
  97. #endif
  98. }
  99.  
  100. /*    XGStdGroupBox::SetText
  101.  *
  102.  *        Set the text and force a redraw
  103.  */
  104.  
  105. void XGStdGroupBox::SetText(char *text)
  106. {
  107. #if OPT_MACOS == 1
  108.     fString = text;
  109. #endif
  110.  
  111. #if OPT_WINOS == 1
  112.     ::SetWindowText(_GetHWND(),text);
  113. #endif
  114.  
  115.     InvalView();
  116. }
  117.  
  118. /************************************************************************/
  119. /*                                                                        */
  120. /*    Static Control Drawing                                                */
  121. /*                                                                        */
  122. /************************************************************************/
  123.  
  124. /*    XGStdGroupBox::DoDrawView
  125.  *
  126.  *        Draw this thing
  127.  */
  128.  
  129. void XGStdGroupBox::DoDrawView(Rect)
  130. {
  131. #if OPT_MACOS == 1
  132.     XGDraw draw(this);
  133.     Rect r;
  134.     Rect s;
  135.     unsigned char buffer[256];
  136.     short i;
  137.     FontInfo finfo;
  138.     
  139.     r = GetContentRect();
  140.     cvtc2p(buffer,fString.Get());
  141.     ::TextFont(fFont);
  142.     ::TextSize(fSize);        // request current font and size
  143.     ::EraseRect(&r);
  144.     ::GetFontInfo(&finfo);
  145.     
  146.     // Calculate the rectangle things go into
  147.     s.left = r.left + 8;
  148.     s.right = r.right - 8;
  149.     i = s.left + ::StringWidth(buffer);
  150.     if (s.right > i) s.right = i;
  151.     s.top = r.top;
  152.     s.bottom = s.top + finfo.ascent + finfo.descent;
  153.     r.top += finfo.ascent - 3;
  154.     
  155.     // Draw the box
  156.     ::MoveTo(s.left-3,r.top);
  157.     ::LineTo(r.left,r.top);
  158.     ::LineTo(r.left,r.bottom-1);
  159.     ::LineTo(r.right-1,r.bottom-1);
  160.     ::LineTo(r.right-1,r.top);
  161.     ::LineTo(s.right+3,r.top);
  162.     
  163.     // Clip and draw the text
  164.     r.right = s.right;
  165.     r.top = s.top;
  166.     draw.ClipRect(&r);
  167.     ::MoveTo(s.left,s.top+finfo.ascent);
  168.     ::DrawString(buffer);
  169. #endif
  170. }
  171.  
  172. /************************************************************************/
  173. /*                                                                        */
  174. /*    Internal Initialization                                                */
  175. /*                                                                        */
  176. /************************************************************************/
  177.  
  178. /*    XGStdGroupBox::Init
  179.  *
  180.  *        Initialization central
  181.  */
  182.  
  183. void XGStdGroupBox::Init(char *buffer)
  184. {
  185.     long l;
  186.     
  187.     l = GetParent()->ReceiveDispatch(KEventGetFont,GetViewID(),(void *)&this);
  188. #if OPT_MACOS == 1
  189.     fFont = GETHIWORD(l);
  190.     fSize = GETLOWORD(l);
  191.     fString = buffer;
  192. #endif
  193.  
  194. #if OPT_WINOS == 1
  195.     Rect r;
  196.     HWND w;
  197.     
  198.     /*
  199.      *    Get the location of the control in the parent's coordinate
  200.      *    system
  201.      */
  202.      
  203.     r = GetContentRect();
  204.     if (GetParent()) {
  205.         ViewToGlobal(&r);
  206.         GetParent()->GlobalToView(&r);
  207.     }
  208.     
  209.     /*
  210.      *    Create the control window
  211.      */
  212.     
  213.     w = CreateWindow("BUTTON",                    // control class
  214.                      buffer,                    // control title
  215.                      WS_CHILD | BS_GROUPBOX,    // control window flags
  216.                      r.left,
  217.                      r.top,
  218.                      r.right-r.left,
  219.                      r.bottom-r.top,            // control location
  220.                      GetParent()->_GetHWND(),    // container window
  221.                      NULL,                        // no menu
  222.                      _GInstance,                // My app instance
  223.                      NULL);                        // no additional args
  224.     if (w == NULL) {
  225.         throw XPostError("Unable to make group box");
  226.     }
  227.     
  228.     _SetHWND(w);                                // set me as the window
  229.     SetProp(w,_GViewClass,(HANDLE)this);
  230.     if (IsVisible()) ShowWindow(w,SW_SHOW);
  231.     EnableWindow(w,IsEnabled());                // set me up
  232. #endif
  233. }
  234.  
  235.